﻿using AutoMapper;
using AutoMapper.Configuration;
using System;
using System.Collections.Generic;
using System.Reflection;

namespace AutoMapper.DTO
{
    public class Mappings
    {
        /// <summary>
        /// Mappings初始化
        /// </summary>
        public static void RegisterMappings()
        {
            var types = Assembly.Load("AutoMapper.DTO").GetTypes();

            var cfg = new MapperConfigurationExpression();

            foreach (var typeInfo in types)
            {
                if (typeInfo.Name.EndsWith("Profile"))
                {
                    cfg.AddProfile(typeInfo);
                }
            }
            Mapper.Initialize(cfg);
        }


        /// <summary>
        /// 获取需要映射的Profile
        /// </summary>
        /// <returns></returns>
        public static Type[] GetProfileTypes()
        {
            List<Type> profileTypes = new List<Type>();
           
            var types = Assembly.Load("AutoMapper.DTO").GetTypes();

            foreach (var typeInfo in types)
            {
                if (typeInfo.Name.EndsWith("Profile"))
                {
                    profileTypes.Add(typeInfo);
                }
            }
            return profileTypes.ToArray();
        }
    }
}
